﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RedBit.OAuthLibrary;
using TradeMeSDK.OAuthLibrary;

namespace TradeMeSDK
{
    public class TradeMeRepository
    {
        private TradeMeCatalog _catalog;
        private TradeMeMembership _membership;
        private TradeMeSearch _search;
        private TradeMeMyTradeMe _myTradeMe;

        public TradeMeCatalog Catalog
        {
            get 
            { 
                if (_catalog == null) _catalog = new TradeMeCatalog();
                return _catalog; 
            }
        }

        public TradeMeMembership Membership
        {
            get
            {
                if (_membership == null) _membership = new TradeMeMembership();
                return _membership;
            }
        }

        public TradeMeSearch Search
        {
            get
            {
                if (_search == null) _search = new TradeMeSearch();
                return _search;
            }
        }

        public TradeMeMyTradeMe MyTradeMe
        {
            get
            {
                if (_myTradeMe == null) _myTradeMe = new TradeMeMyTradeMe();
                return _myTradeMe;
            }
        }

        public bool IsLoggedIn
        {
            get
            {
                return !string.IsNullOrEmpty(TradeMeOAuth.Verifier);
            }
        }

        private static Uri _baseUri;
        public static Uri BaseUri
        {
            get { return _baseUri ?? (_baseUri = new Uri(TradeMeConstants.BaseUri)); }
            set { _baseUri = value; }
        }

        private static Uri _baseSecureUri;
        public static Uri BaseSecureUri
        {
            get { return _baseSecureUri ?? (_baseSecureUri = new Uri(TradeMeConstants.BaseSecureUri)); }
            set { _baseSecureUri = value; }
        }




        public Category FindCategory(string cat)
        {
            Category c = Catalog.RootCategory;
            Category sub;

            do
            {
                if (c.Subcategories == null) break;
                sub = c.Subcategories.Where(subcat => cat.StartsWith(subcat.Number)).FirstOrDefault();
                if (sub != null)
                    c = sub;
            } while (sub != null);
            return c;
        }

        public string CategoryName(string cat)
        {
            return FindCategory(cat).Name;
        }

        public List<Category> CategoryTree(string cat)
        {
            // Category="0002-0352-9333-9391-"
            // CategoryPath="/Computers/Apple/iPads/Cases-protectors"

            var catList = new List<Category>();
            while (cat.Length > 0)
            {
                catList.Add(FindCategory(cat));

                cat = cat.Substring(0, cat.Length - 1);
                var index = cat.LastIndexOf('-');
                if (index <= 0) break;
                cat = cat.Substring(0, index + 1);
            }

            catList.Reverse();
            return catList;
        }

    }


}
